package com.jobcn.common.util;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * Created by winson on 17-6-21.
 */
public class StringUtil {

    /**
     * 取两个字符串中间的内容
     * abc234cbd    sub("abc23492cbd","abc","cba")  返回234
     *
     * @param origin 原字符串
     * @param start  前字符串
     * @param end    后字符串
     * @return
     */
    public static String sub(String origin, String start, String end) {
        int startIndex = origin.indexOf(start) + start.length();
        int endIndex = origin.indexOf(end);
        return origin.substring(startIndex, endIndex);
    }

    /**
     * list转成字符串,通过分号分割
     *
     * @param list
     * @return
     */
    public static String list2string(List<String> list) {
        StringBuilder stringBuilder = new StringBuilder();
        for (String str : list) {
            stringBuilder.append(str);
            stringBuilder.append(";");
        }
        return stringBuilder.toString();
    }


    /**
     * 读取json格式的文件转成Map
     *
     * @param jsonFilePath
     * @return
     */
    public static Map<String, Object> ReadJsonFile(String jsonFilePath) {
        BufferedReader reader = null;
        String laststr = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(jsonFilePath);
            InputStreamReader inputStreamReader = new InputStreamReader(
                    fileInputStream, "utf-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return (Map<String, Object>) JSON.parseObject(laststr, Map.class);
    }
}
